package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/5.
 */

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzModule;
import com.xianzhiOA.bean.XzProject;
import com.xianzhiOA.bean.XzStage;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzModuleService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 模块的模型层
 *
 * @author 甘汝雷
 * @create 2017-05-05 16:43
 **/
@Controller
@RequestMapping("/Module")
@SessionAttributes("userLogin")
public class XzModuleController {
    @Autowired
    private XzModuleService moduleService;
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzEmpService empService;
    @Autowired
    private XzWorkService workService;

    //添加模块
    @RequestMapping("addModule.emp")
    public ModelAndView addModule(XzModule module, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = errors(module);
        if (mv.getViewName().equals("backEnd/moduleEdit"))return mv;
        if (module.getModuleEmp()==null) module.setModuleEmp(emp.getEmpId());
        moduleService.insert(module);
        return mv;
    }
    //表单验证
    public ModelAndView errors(XzModule module){
        ModelAndView mv = new ModelAndView("redirect:selModuleAll.emp");
        Map errors = new HashMap();
        int a = 0;
        if (module.getModuleName()==null||module.getModuleName()=="") errors.put("moduleName","模块名不能为空");
        if (module.getStageList()!=null){
            for (XzStage stage:module.getStageList()){
                module.setModulePlan(stage.getStagePlan());
                module.setModuleActual(stage.getStageActual());
                if (a==0)module.setModuleBeginTime(stage.getStageWeek());
                module.setModuleTime(stage.getStageWeek());
                a++;
            }
        }
        if (module.getStageList1()!=null){
            if (module.getStageList()==null){
                module.setStageList(new ArrayList<>());
            }
            for (int i=0;i<module.getStageList1().size();i=i+3){
                String plan = (String)module.getStageList1().get(i);
                String actual = (String)module.getStageList1().get(i+1);
                String stageWeek = (String)module.getStageList1().get(i+2);
                XzStage stage = new XzStage();
                stage.setStagePlan(Integer.valueOf(plan));
                stage.setStageActual(Integer.valueOf(actual));
                stage.setStageWeek(myDate(stageWeek));
                module.getStageList().add(stage);
                module.setModulePlan(stage.getStagePlan());
                module.setModuleActual(stage.getStageActual());
                if (a==0)module.setModuleBeginTime(stage.getStageWeek());
                module.setModuleTime(stage.getStageWeek());
                a++;
            }
        }
        if (!errors.isEmpty()){
            mv.addObject("errors",errors);
            mv.addObject("module",module);
            mv.addObject("stageSize",module.getStageList().size());
            mv.addObject("projectList",projectService.selAll(new XzProject()));
            mv.addObject("empList",empService.selAll(new XzEmp()));
            mv.setViewName("backEnd/moduleEdit");
        }
        return mv;
    }
    //把字符串类型转换成Date
    public Date myDate(String string){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date date = null;
        try {
            date=sdf.parse(string);
        }catch (Exception e){

        }
        return date;
    }
    //删除模块
    @RequestMapping("deleteModule.emp")
    public ModelAndView deleteModule(Long moduleId){
        ModelAndView mv = new ModelAndView("redirect:selModuleAll.emp");
        moduleService.isdelete(moduleId);
        return mv;
    }
    //编辑模块
    @RequestMapping("moduleEdit.emp")
    public ModelAndView moduleEdit(Long moduleId,@ModelAttribute("userLogin")XzEmp emp){
        //加载项目
        ModelAndView mv = new ModelAndView("backEnd/moduleEdit");
        XzModule module = new XzModule();
        if (moduleId!=null&&moduleId!=0) {
            module = moduleService.selById(moduleId);
            mv.addObject("stageSize",module.getStageList().size());
            mv.addObject("stageId",module.getStageList().get(0).getStageId());
        }else module.setModuleId(0l);
        mv.addObject("module",module);
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        mv.addObject("empList",empService.selAll(new XzEmp()));
        return mv;
    }
    //修改模块
    @RequestMapping("updateModule.emp")
    public ModelAndView updateModule(XzModule module){
        ModelAndView mv = errors(module);
        mv.addObject("module", module);
        if (mv.getViewName().equals("backEnd/moduleEdit")) return mv;
        moduleService.update(module);
        return mv;
    }

    //分页查询模块
    @RequestMapping("selModuleAll.emp")
    public ModelAndView selModuleAll(XzModule module, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "8") int rows,
                                     Date begin,Date end, @ModelAttribute("userLogin")XzEmp emp){
        ModelAndView mv = new ModelAndView("backEnd/moduleShow");
        if (module.getModuleEmp()==null && !emp.getEmpRole().equals("1")) module.setModuleEmp(emp.getEmpId());
        PageBean<XzModule> pageBean = moduleService.selAll(module, page, rows, begin, end);
        List<XzModule> moduleList = pageBean.getList();
        for (XzModule m:moduleList) {
            Map byDate = workService.getWeekByDate(m.getModuleTime());
            Map byDate1 = workService.getWeekByDate(m.getModuleBeginTime());
            m.setModuleTimeBegin((String)byDate1.get("begin"));
            m.setModuleTimeEnd((String) byDate.get("end"));
        }
        List<XzProject> projects = projectService.selAll(new XzProject());
        mv.addObject("empList",empService.selAll(new XzEmp()));
        mv.addObject("projectList",projects);
        mv.addObject("moduleList",pageBean.getList());
        mv.addObject("module",module);
        mv.addObject("page",pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("begin",begin);
        mv.addObject("end",end);
        return mv;
    }
    //通过指定日期查询一周的模块信息
    public List<XzModule> moduleList(List<XzModule> moduleList){
        for (XzModule m:moduleList) {
            Map byDate = workService.getWeekByDate(m.getModuleTime());
            m.setModuleTimeBegin((String)byDate.get("begin"));
            m.setModuleTimeEnd((String)byDate.get("end"));
        }
        return moduleList;
    }
    //通过id查询
    @RequestMapping("selById.emp")
    public ModelAndView selById(Long moduleId){
        ModelAndView mv = new ModelAndView("backEnd/moduleShow");
        XzModule module = moduleService.selById(moduleId);
        if (module.getStageList()!=null){
            for (XzStage s:module.getStageList()) {
                Map byDate = workService.getWeekByDate(s.getStageWeek());
                s.setStageWeekBegin((String)byDate.get("begin"));
                s.setStageWeekEnd((String) byDate.get("end"));
            }
        }
        mv.addObject("module",module);
        mv.addObject("open",1);
        return mv;
    }
    @ResponseBody
    @RequestMapping("selectByModuleName.emp")
    public boolean selectByModuleName(String moduleName,Long moduleProject){
        Map map = new HashMap();
        map.put("moduleName",moduleName);
        map.put("moduleProject",moduleProject);
        XzModule module = moduleService.selectByModuleName(map);
        if (module==null) return false;
        return true;
    }

    @RequestMapping("stageIsdelete.emp")
    public ModelAndView stageIsdelete(long stageId,long moduleId){
        ModelAndView mv = new ModelAndView("backEnd/moduleEdit");
        moduleService.stageIsdelete(stageId);
        XzModule module = moduleService.selById(moduleId);
        mv.addObject("stageSize", module.getStageList().size());
        mv.addObject("stageId", stageId);
        mv.addObject("module",module);
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        mv.addObject("empList",empService.selAll(new XzEmp()));
        return mv;
    }
}
